package cn.cuilan.test.thread;

/**
 * Join方式实现
 *
 * @author zhangyan
 * @since 2023/3/10
 */
public class JoinPrint implements Runnable {

    private final Thread beforeThread;

    public JoinPrint(Thread beforeThread) {
        this.beforeThread = beforeThread;
    }

    @Override
    public void run() {
        if (beforeThread == null) {
            System.out.println(Thread.currentThread().getName());
        } else {
            try {
                beforeThread.join();
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new JoinPrint(null), "A");
        Thread t2 = new Thread(new JoinPrint(t1), "B");
        Thread t3 = new Thread(new JoinPrint(t2), "C");

        t1.start();
        t2.start();
        t3.start();
        Thread.sleep(1000L);
    }

}
